package com.zjhcsoft.auth.controller;


import com.dc.eai.data.CompositeData;
import com.dcfs.esb.utils.TLSimRepTools;
import com.zjhcsoft.auth.common.enums.ApiCode;
import com.zjhcsoft.auth.common.utils.ResultUtil;
import com.zjhcsoft.auth.common.vo.Result;
import com.zjhcsoft.auth.entity.web.*;
import com.zjhcsoft.auth.service.AccountService;
import com.zjhcsoft.auth.service.PasswordPolicyService;
import com.zjhcsoft.auth.service.PasswordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * <p>
 * 账号表 前端控制器
 * </p>
 *
 * @author wzya
 * @since 2021-03-29
 */
@Controller
@RequestMapping("/account")
@Api("账户管理接口控制器")
public class AccountController {

    @Autowired
    AccountService accountService;
    @Autowired
    PasswordService passwordService;
    @Autowired
    PasswordPolicyService passwordPolicyService;

 /*   @PostMapping("/update/mobile")
    @ResponseBody
    @ApiOperation(value="重新绑定手机号")
    public Result updateMobile(UpdateMobileRequest request) {
        return accountService.updateMobile(request);
    }*/

/*     @PostMapping("/update/device")
    @ResponseBody
    @ApiOperation(value="设备绑定")
    public Result updateDevice(UpdateDeviceRequest request) {
        return accountService.updateDevice(request);
    }*/

    @PostMapping("/query/device")
    @ResponseBody
    @ApiOperation(value="查询绑定设备列表")
    public String queryDevice(HttpServletRequest request, HttpServletResponse respons) {
        String resultMsg="处理成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            //String  x  = new String(IOUtils.toByteArray(is),"UTF-8");
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            QueryDeviceRequest queryDeviceRequest = (QueryDeviceRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = accountService.queryDevice(queryDeviceRequest);
            result.setType(ApiCode.DEVICE.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }
/*
    @PostMapping("/update/password")
    @ResponseBody
    @ApiOperation(value="修改密码")
    public Result updatePassword(UpdatePasswordRequest request) {
        return accountService.updatePassword(request);
    } */

    @PostMapping("/query/check_type")
    @ResponseBody
    @ApiOperation(value="密码校验方式查询")
    public String queryCheckType(HttpServletRequest request, HttpServletResponse respons) {
        String resultMsg="处理成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            QueryCheckTypesRequest queryCheckTypesRequest = (QueryCheckTypesRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = accountService.queryCheckType(queryCheckTypesRequest);
            result.setType(ApiCode.QUERY_CHECK_TYPES.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }

    @PostMapping("/update/check_type")
    @ResponseBody
    @ApiOperation(value="开启/关闭密码校验方式")
    public String updateCheckType(HttpServletRequest request, HttpServletResponse respons) {
        String resultMsg="处理成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            UpdateCheckTypeRequest updateCheckTypeRequest = (UpdateCheckTypeRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = accountService.updateCheckType(updateCheckTypeRequest);
            result.setType(ApiCode.UPDATE_CHECK_TYPES.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }
/*
   @PostMapping("/query/credential")
    @ResponseBody
    @ApiOperation(value="登录方式查询")
    public Result queryCredential(QueryCredentialRequest request) {
        return accountService.queryCredential(request);
    }*/

/*    @PostMapping("/update/credential")
    @ResponseBody
    @ApiOperation(value="登录方式开启/关闭")
    public Result updateCredential(UpdateCredentialRequest request) {
        return accountService.updateCredential(request);
    }*/

    @PostMapping("/query/userinfo")
    @ResponseBody
    @ApiOperation(value="用户信息查询")
    public String queryUserInfo(HttpServletRequest request, HttpServletResponse respons) {
        String resultMsg="处理成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            QueryUserInfoRequest queryUserInfoRequest = (QueryUserInfoRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = accountService.queryUserInfo(queryUserInfoRequest);
            result.setType(ApiCode.USET_INFO.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }

    @PostMapping("/query/corporation")
    @ResponseBody
    @ApiOperation(value="多法人信息查询")
    public String queryCorporation(HttpServletRequest request, HttpServletResponse respons) {
        String resultMsg="处理成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            QueryCorporationRequest queryCorporationRequest = (QueryCorporationRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = accountService.queryCorporation(queryCorporationRequest);
            result.setType(ApiCode.QUERY_CORPORATION.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }

/*    @PostMapping("/device/certificate")
    @ResponseBody
    @ApiOperation(value="设备安全认证")
    public Result deviceCertificate( ) {
        //TODO..
        return Result.ok();
    }*/

/*    @PostMapping("/password/complexity/certificate")
    @ResponseBody
    @ApiOperation(value="密码复杂度校验")
    public Result passwordComplexityCertificate(String password ) {
        //TODO..
        return Result.ok(passwordService.passwordComplexityValidate(password));
    }*/


}

